import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';

Dio dio = new Dio();

class BottomInputDialog extends StatefulWidget {
  //评论界面
  String videoId; //上个界面传过来的内容的id
  BottomInputDialog({Key key, @required this.videoId}) : super(key: key);
  _BottomInputDialogState createState() =>
      _BottomInputDialogState(this.videoId);
}

class _BottomInputDialogState extends State<BottomInputDialog> {
  String videoId;
  _BottomInputDialogState(this.videoId);
  TextEditingController _comment = TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getId();
    missionpoint();
    // message();
  }

  var _point;
  void missionpoint() async {
    if (number.length == 10) {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${this.user_id}');
      _point = response.data;
    } else {
      Response response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${this.user_id}');
      _point = response.data;
    }
    setState(() {});
  }

  var user_id;
  var number;
  getId() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');
    number = sharedPreferences.getString('number');
    missionpoint();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.transparent,
      body: new Column(
        children: <Widget>[
          Expanded(
              child: new GestureDetector(
            child: new Container(
              color: Colors.black54,
            ),
            onTap: () {
              Navigator.pop(context);
            },
          )),
          new Container(
            height: 40,
            color: Colors.white,
            alignment: Alignment.centerLeft,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width * 0.8,
                  child: TextField(
                    inputFormatters: [
                      BlacklistingTextInputFormatter(RegExp("[ ]")),
                    ],
                    enableInteractiveSelection: true,
                    controller: _comment,
                    maxLines: null,
                    autofocus: true,
                    cursorColor: Colors.red,
                    decoration: InputDecoration(
                        contentPadding: new EdgeInsets.all(
                          5.0,
                        ),
                        border: InputBorder.none,
                        hintText: "欢迎发表你的评论",
                        hintStyle:
                            new TextStyle(fontSize: 14, color: Colors.black)),
                    style: new TextStyle(
                      fontSize: 16,
                      color: Colors.black,
                    ),
                  ),
                ),
                InkWell(
                    onTap: () async {
                      Dio dio = new Dio();
                      if (_comment.text.length == 0) {
                        showDialog<Null>(
                          context: context,
                          barrierDismissible: false,
                          builder: (BuildContext context) {
                            return new AlertDialog(
                              title: new Text('温馨提示'),
                              content: new SingleChildScrollView(
                                child: new ListBody(
                                  children: <Widget>[
                                    new Text('评论内容不能为空'),
                                  ],
                                ),
                              ),
                              actions: <Widget>[
                                new FlatButton(
                                  child: new Text('确定'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                              ],
                            );
                          },
                        );
                      } else {
                        String userInfo = "${this._comment.text}";
                        Response response = await dio.post(
                            "http://xssw.gdit.edu.cn/sizheng/api/v1/addComment",
                            data: {
                              "contain": "${this._comment.text}", //评论内容
                              "number": "${this.number}", //学号
                              "videoId": "${this.videoId}" //内容id
                            });
                        if (number.length == 10) {
                          if (_point['mission']['viewpoint'] == 'false') {
                            
                            Navigator.pop(context, userInfo);
                            var newpoint = _point['point'] + 1;
                            var pp = await dio.patch(
                                'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
                                data: {'point': newpoint});
                            var zhuangtai = await dio.patch(
                                'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
                                data: {
                                  "mission": {
                                    "viewpoint": "true",
                                  }
                                });
                          } else {
                            Navigator.pop(context, userInfo);
                          }
                        } else {
                          if (_point['mission']['viewpoint'] == 'false') {
                            var newpoint = _point['point'] + 1;
                            var pp = await dio.patch(
                                'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
                                data: {'point': newpoint});
                            var zhuangtai = await dio.patch(
                                'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
                                data: {
                                  "mission": {
                                    "viewpoint": "true",
                                  }
                                });
                            Navigator.pop(context, userInfo);
                          } else {
                            Navigator.pop(context, userInfo);
                          }
                        }
                      }
                    },
                    child: Container(
                      margin: EdgeInsets.all(5),
                      alignment: Alignment.center,
                      // decoration: BoxDecoration(
                      //   borderRadius: BorderRadius.circular(5),),
                      color: Colors.red,
                      height: 30.0,
                      width: MediaQuery.of(context).size.width * 0.15,

                      child: Text(
                        "发布",
                        style: TextStyle(color: Colors.white),
                      ),
                    )),
              ],
            ),
          )
        ],
      ),
    );
  }
}

class PopRoute extends PopupRoute {
  final Duration _duration = Duration(milliseconds: 300);
  Widget child;

  PopRoute({@required this.child});

  @override
  Color get barrierColor => null;

  @override
  bool get barrierDismissible => true;

  @override
  String get barrierLabel => null;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return child;
  }

  @override
  Duration get transitionDuration => _duration;
}
